using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using ipi.MCC.Global;

namespace ipi.MCC.Controls
{
    public class MCCTreeStartPositionControl : MCCAjaxBaseControl
	{
        private string sPreSelectedNodeID;
        protected CustomValidator validGlobal;
        protected MCCTreeControl tcMain;
        protected MCCTreePath tpNavigation;
        protected HiddenField hiddenNodeID;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            tcMain.OnNodeSelected += new OnNodeSelectedHandler(tcMain_OnNodeSelected);
            tpNavigation.OnNavigationSelected += new OnNavigationSelectedHandler(tpNavigation_OnNavigationSelected);

            tcMain.PreSelected = sPreSelectedNodeID;

            SPSite spsCurrent = SPContext.Current.Site;
            WebSettings wsCurrent = new WebSettings(spsCurrent);

            using (SPSite spsRoot = new SPSite(!string.IsNullOrEmpty(wsCurrent.MasterWebURL) ? wsCurrent.MasterWebURL : spsCurrent.Url))
            {
                SPWeb spwRoot = spsRoot.RootWeb;
                SPList splNodes = Functions.GetTreeList(spwRoot);
                tcMain.CreateTree(splNodes);
            }            
        }

        protected void tpNavigation_OnNavigationSelected(int iNodeID)
        {
            tcMain.SelectNode(iNodeID);
            hiddenNodeID.Value = iNodeID.ToString();
        }

        protected void tcMain_OnNodeSelected(TreeNode CurrentNode)
        {
            tpNavigation.SetTreePath(CurrentNode);
            hiddenNodeID.Value = CurrentNode.Value;
        }

        public void PreSelectNode(string NodeID)
        {
            sPreSelectedNodeID = NodeID;
        }
        
        public int GetStartNodeID()
        {
            int iStartNodeID = -1;

            if (!string.IsNullOrEmpty(hiddenNodeID.Value))
            {
                iStartNodeID = Convert.ToInt32(hiddenNodeID.Value);
            }
            
            return iStartNodeID;
        }


	}
}